﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// ImagePreviewWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ImagePreviewWindow : MetroWindow
    {
        private string imageFilePath;
        /// <summary>
        /// [只读]取当前预览的图像文件的完整磁盘路径。
        /// </summary>
        public string ImageFilePath { get { return imageFilePath; } }

        public string ImageTitle
        {
            get
            {
                return this.imageTitle;
            }

            set
            {
                this.imageTitle = value;
                tbImageTitle.Text = this.imageTitle;
            }
        }

        private string imageTitle = "";

        /// <summary>
        /// [构造方法]用于预览图像的窗口。
        /// </summary>
        /// <param name="imageFilePath">图像文件完整路径。</param>
        /// <param name="imageTitle">图像文件应显示的Title。</param>
        public ImagePreviewWindow(string imageFilePath, string imageTitle)
        {
            InitializeComponent();

            this.imageFilePath = imageFilePath;
            this.imageTitle = imageTitle;

            tbImageTitle.FontFamily = Globals.MainWindow.mainTabControl.FontFamily;
            tbImageTitle.FontSize = Globals.MainWindow.mainTabControl.FontSize;

            if (WorkspaceTreeViewItem.IsValidateImageFilePath(imageFilePath))
            {
                previewImage.Source = L.BuildBitmapImage(imageFilePath, out Size size);  // 右边这种写法会导致图像文件被锁定。new BitmapImage(new Uri(imageFilePath));
                previewImage.Width = size.Width;
                previewImage.Height = size.Height;
            }

            tbImageTitle.Text = this.imageTitle;
            if (string.IsNullOrWhiteSpace(imageTitle))
            {
                gsTitle.Visibility =
                    tbImageTitle.Visibility = Visibility.Collapsed;
            }
            else
            {
                gsTitle.Visibility =
                    tbImageTitle.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// Esc 键关闭窗口退出预览。
        /// </summary>
        private void MetroWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape) this.Close();
        }

        private void PreviewImage_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            // 缩放图片
            var st = (ScaleTransform)previewImage.RenderTransform;
            double zoom = e.Delta > 0 ? .2 : -.2;
            st.ScaleX += zoom;
            st.ScaleY += zoom;
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var st = (ScaleTransform)previewImage.RenderTransform;
            var scX = (this.ActualWidth - 20) / previewImage.ActualWidth;
            var scY = (this.ActualHeight - 60) / previewImage.ActualHeight;  // 注意：要去掉标题

            st.ScaleX = st.ScaleY = Math.Min(scX, scY);
        }
    }
}
